/**
 * Created by xudao on 16/1/14.
 */

(function($) {
	jQuery.extend({
		'loading' : {
			openLoading : function() {
				this.removeLoading();
				var html = '<div class="aui-toast"  style="display:block" id="loading">' + '<div class="aui-toast-loading"></div>' + '<div class="aui-toast-content">加载中</div></div>';
				$('body').append(html);
			},
			removeLoading : function() {
				$('#loading').remove();
			},
			closeLoading : function() {
				this.removeLoading();
			},
			showLoading : function() {
				$('#loading').show();
			},
			hideLoading : function() {
				$('#loading').hide();
			}
		},

		'default' : {
			openDefault : function(data) {
				this.removeDefault();
				var title = data ? data : '提交成功';
				var html = '<div class="aui-toast" style="display:block" id="default"><i class="aui-iconfont aui-icon-check"></i><div class="aui-toast-content">' + title + '</div></div>';
				$('body').append(html);
				this.showDefault();
			},
			removeDefault : function() {
				$('#default').remove();
			},
			closeDefault : function() {
				this.removeDefault();
			},
			showDefault : function() {
				$('#default').show();
			},
			hideDefault : function() {
				$('#default').hide();
			}
		},

		'confirm' : {
			openConfirm : function(data, callback) {
				var html = '<div class="overflow-container confirm-div"><div class="confirm-container"><div class="confirm-title">' + data.title + '</div><div class="confirm-content">' + data.content + '</div><div class="confirm-btn-group"><div class="confirm-btn cancel-btn">' + data.cancelBtnText + '</div><div class="confirm-btn cancel-btn">' + data.submitBtnText + '</div></div></div></div>';
				$('body').append(html);
				$('.confirm-btn').off().on('click', function() {
					var index = $(this).index();
					$('.confirm-div').remove();
					callback && callback(index);
				})
			},
			removeConfirm : function() {
				$('.confirm-div').remove();
			},
			closeConfirm : function() {
				this.removeDefault();
			},
			showConfirm : function() {
				$('.confirm-div').show();
			},
			hideConfirm : function() {
				$('.confirm-div').hide();
			}
		},

		'alert' : {
			openAlert : function(data, callback) {
				var html = '<div class="overflow-container alert-div"><div class="alert-container"><div class="alert-title">' + data.title + '</div><div class="alert-content">' + data.content + '</div><div class="alert-btn-group"><div class="alert-btn cancel-btn">' + data.btntext + '</div></div></div></div>';
				$('body').append(html);
				$('.alert-btn').off().on('click', function() {
					$('.alert-div').remove();
					callback && callback();
				})
			},
			removeAlert : function() {
				$('.alert-div').remove();
			},
			closeAlert : function() {
				this.removeDefault();
			},
			showAlert : function() {
				$('.alert-div').show();
			},
			hideAlert : function() {
				$('.alert-div').hide();
			}
		},

		'promot' : {
			openPromot: function(data, callback) {
				var html = '<div class="overflow-container promot-div"><div class="promot-container"><div class="promot-title">' + data.title + '</div><div class="promot-content"><input type="text" placeholder="'+data.placeholder+'" id="returnNum"></div><div class="promot-btn-group"><div class="promot-btn cancel-btn">' + data.btntext + '</div></div></div></div>';
				$('body').append(html);
				$('.promot-btn').off().on('click', function() {
					var data = $('#returnNum').val();
					$('.promot-div').remove();
					callback && callback(data);
				})
			},
			removePromot : function() {
				$('.promot-div').remove();
			},
			closePromot : function() {
				this.removeDefault();
			},
			showPromot : function() {
				$('.promot-div').show();
			},
			hidePromot : function() {
				$('.promot-div').hide();
			}
		},

		'range': {
			range: function(element,callback){
                var time=null;
                var distince,offsetLeft,tooltipWidth;
                var _init = function(){
                    distance = Math.abs(element.max - element.min);
                    offsetLeft = element.offsetLeft;
                    tooltipWidth = element.offsetWidth - 28;
                    element.insertAdjacentHTML('afterend','<div class="aui-range-tip aui-hide">'+element.value+'</div>');
                    var scaleWidth = (tooltipWidth / distance) * Math.abs(element.value - element.min);
                    element.nextSibling.style.left = (offsetLeft + scaleWidth - 11)+'px';
                    element.addEventListener("input",function(){
                        _showTip();
                    });
                    element.addEventListener("touchmove",function(){
                        _showTip();
                    });
                    element.addEventListener("touchend",function(){
                        _hideTip();
                    });
                }
                var _showTip = function(){
                    element.nextSibling.classList.remove("aui-hide");
                    var scaleWidth = (tooltipWidth / distance) * Math.abs(element.value - element.min);
                    element.nextSibling.style.left = (offsetLeft + scaleWidth - 11)+'px';
                    element.nextSibling.innerText = element.value;
                    callback(element.value);
                }
                var _hideTip = function(){
                    if (time) {
                        clearTimeout(time);
                    }
                    time = setTimeout(function() {
                        element.nextSibling.classList.add("aui-hide");
                    }, 1500);
                }
                _init();
            }
		},

        'waterfall': {
            waterfall: function(el, opts){
                var col=2,
                    padding=15,
                    space=15;
                var _setting = function(){
                    col = opts.col?opts.col:col;
                    padding = opts.padding?opts.padding:padding;
                    space = opts.space?opts.space:space;
                }
                var _init = function(){
                    var el_w = $(el).width();
                    var list_w = (el_w/col)-padding-space;
                    $(el).css({'-webkit-column-width': list_w+'px','-webkit-column-count': col,'padding':padding+'px','-webkit-column-gap':space+'px'});

                }
                _setting();
                _init();
            }
        }
	});
})(jQuery);
